<template>
	<view class="my-record" ref="uv-back-top">
		<headers></headers>
		<el-container>
			<view class="hidden-sm-and-down" style="max-width:500px">
				<my :heads="false"></my>
			</view>
			<!--height: calc(100vh - 60px);overflow:auto :title="item.nickname" :note="item.msg"
			:thumb="item.portrait" thumb-size="lg" :rightText="ctime(item.createdAt)" -->
			<el-main style="padding: 0px;margin: 0px;max-width: 900px;">
				<view style="margin: 5px;border-radius: 5px;border: 1px solid #eee;">
					<view style="display: flex;justify-content: space-between;align-items: center;">
						<uv-tabs :list="[...uvTabs]" @click="click"></uv-tabs>
					</view>
<!-- , {
						name: '新增文章',
						value: 5,
						badge: {
							isDot: true
						}
					} -->
					<uv-list v-if="list.length>0" @scrolltolower="loadMore" class="record_content infinite-list"
						v-infinite-scroll="loadMore" :height="300">
						<uv-list-item v-for="(item,index) in list" :key="item.createdAt">
							<view class="record_list">
								<view style="display: flex;font-size: 16px;font-weight: bold;">
									{{item.name}}<el-tag style="margin-left: 10px;"
										size="mini">{{typed(item.type)}}</el-tag>
								</view>
								<view style="display: flex;justify-content: space-between;align-items: center;">
									<view
										style="display: flex;align-content: flex-start;align-items: flex-start;flex-wrap: wrap;">
										<uv-text lines="4" color="#333" size="14" :text="item.des"></uv-text>
										<!-- titlePrompt(item) -->
										<!-- <view style="margin: 0px 5px  5px" v-for="(ite,ind) in uv_image(item)"
											:key="ind">
											<uv-image :src="ite+ '?x-oss-process=image/resize,w_100,m_lfit/format,webp'"
												width="80px" height="80px"></uv-image>
										</view> -->
									</view>
									<view style="">
										<view style="display: flex;justify-content: flex-end;align-items: center;">
											<view style="display: flex;justify-content: space-between;">
												<uv-text size="13" text="状态："></uv-text><el-tag type="info" plain
													size="mini">{{state(item.status||3)}}</el-tag>
											</view>
										</view>

										<view
											style="display: flex;justify-content: flex-end;align-items: center;margin-top: 3px;">
											<uv-button v-if="item.type=='article'" size="small" style="margin-right:5px"
												text="编辑" @click="details(item)"></uv-button>
											<uv-button v-else size="small" type="primary" text="详情"
												@click="details(item)"></uv-button>
										</view>
										<uv-text style="min-width:120px;" color="#000000" :size="12"
											:text="ctime(item.createdAt)"></uv-text>
									</view>
								</view>

							</view>
						</uv-list-item>
					</uv-list>
					<uv-load-more v-if="list.length > 0" :status="status" :loading-text="loadingText"
						:loadmore-text="loadmoreText" :nomore-text="nomoreText" @loadmore="loadMore" />
					<el-empty v-else description="什么都没有!"></el-empty>
				</view>

			</el-main>
		</el-container>
		<uv-toast ref="toast"></uv-toast>
		<uv-navbar v-if="!screen" :title="'作品记录'" @leftClick="navigateBack" :placeholder="true"></uv-navbar>
		<uv-back-top :scroll-top="scrollTop"></uv-back-top>
		<tabbars :prop-value="'my'"></tabbars>
	</view>
</template>

<script>
	// 引入
	import config from '@/common/config'
	import tabbars from '@/components/tabbar/tabbar';
	import my from '@/pages/my/index'
	import headers from '@/components/header/header.vue';
	import {
		getName,
		getFlowName,
		getFlowType
	} from '@/common/api.js';
	export default {
		components: {
			// 注册组件
			tabbars,
			headers,
			my
		},
		data() {
			return {
				loading: false,
				pageSize: 15,
				page: 1,
				total: 0, //总条数
				hasMore: true, // 是否有更多数据
				status: 'nomore',

				scrollTop: 0,
				uvTabs: [{
						name: '全部',
						value: null,
					},
					// {
					// 	name: '消耗',
					// 	value: '消耗',
					// }, {
					// 	name: '充值',
					// 	value: '充值',
					// }, {
					// 	name: '返还',
					// 	value: '返还',
					// }, {
					// 	name: '其他',
					// 	value: 'other',
					// },
				],
				uvState: [{
						name: '全部',
						value: null,
					},
					{
						name: '草稿',
						value: 1,
					}, {
						name: '审核中',
						value: 2,
					}, {
						name: '已发布',
						value: 3,
					}
				],
				list: [],
				keyword: '', //搜索框
				type: '全部',
				noMore: false,
				loadingText: '正在加载...',
				loadmoreText: '加载更多',
				nomoreText: '没有更多了'
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			//重新加载
			this.page = 1;
			this.getData() //请求更多数据列表
			uni.stopPullDownRefresh(); //停止刷新
		},
		onReachBottom() {
			this.loadMore()
		},
		onPageScroll: function(e) {
			console.log("滚动距离为：" + e.scrollTop);
			if (this.hasMore) {
				this.getData(true); // 当触底时加载更多数据
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
			// #ifdef APP-NVUE
			this.scrollTop = e.detail.scrollTop;
			// #endif
		},
		computed: {
			disabled() {
				return this.loading || this.noMore
			}
		},
		watch: {
			$route: {
				// immediate: true,
				handler(newRoute, oldRoute) {
					this.list = this.list.filter(item => item.sid !== this.dsgesfv);
				}
			},
		},
		mounted() {
			this.getData();
			getFlowType({
				custom: {
					auth: true,
					// toast: false,
					catch: true,
				},
			}).then(e => {
				e.forEach((item) => {
					this.uvTabs.push({
						name: this.typed(item),
						value: item
					});
				});
			}).catch(e => {
				console.log(e);
			})
		},
		methods: {
			details(item) {
				this.routed('/pages/' + item.type + '/index?id=' + item._id)
			},
			state(e) {
				return this.uvState[e || 0].name;
			},
			typed(e) {
				if (e == 'painting') {
					return '图片';
				} else if (e == 'video') {
					return '视频';
				} else if (e == "music") {
					return '音乐';
				} else if (e == 'chat') {
					return '对话'
				} else if (e == 'fusion') {
					return '统合'
				} else if (e == 'article') {
					return '文章'
				} else {
					return '未知'
				}
			},
			titlePrompt(e) {
				if (!e) return ''; // 添加空值保护
				if (e.des) {
					return e.des;
				}
				if (e.type == 'article') {
					return e.content[0];
				} else {
					if (e.content[0].userPrompt && e.content[0].videoUrl) {
						return e.content[0].userPrompt;
					}
					if (e.content[0].prompt && e.content[0].action) {
						return (e.content[0].prompt).split('--ar')[0];
					}
					if (e.content[0].prompt && e.content[0].mode) {
						return e.content[0].prompt;
					}
					if (e.content[0].revised_prompt && e.content[0].url) {
						return e.content[0].revised_prompt;
					}
					if (e.content[0]?.data && e.content[0].data[0]) {
						return e.content[0].data[0].revised_prompt
					}
					if (e.task.taskInfo.title) {
						return e.task.taskInfo.title;
					}
				}
			},
			uv_image(e) {
				let src = [],
					con = null;
				if (e.content && e.content[0]) {
					con = e.content[0];
				}
				if (e.content && e.content[1]) {
					con = e.content[1];
				}
				if (con?.data && con?.data[0]) {
					src = [con.data[0].url]
				}
				if (con?.imageUrl) {
					src = [con.imageUrl]
				}
				if (con?.videoFirstFrame) {
					src = [con.videoFirstFrame]
				}
				if (e?.targetImage) {
					src = [e.targetImage]
				}
				if (con?.images) {
					src = con.images
				}
				if (con?.url) {
					src = [con.url]
				}
				if (con?.musics) {
					for (var i = 0; i < con.musics.length; i++) {
						src.push(con.musics[i].imageUrl)
					}
				}
				return src;
			},
			click(item) {
				// console.log('item', item);
				if (item.value == 5) {
					this.routed('/pages/fusion/index');
					return;
				}
				this.type = item.value;
				this.page = 1;
				this.getData();
			},
			ctime(time) {
				return uni.$uv.date(new Date(time).getTime(), 'yyyy/mm/dd hh:MM')
			},
			loadMore() {
				if (this.status != 'nomore') {
					this.status = 'loading';
					var allTotal = this.page * this.pageSize
					if (allTotal < this.total) {
						//this.total为请求数据的总条数。只要现有条数小于总条数就执行一下代码
						this.page++;
						this.getData(true) //请求更多数据列表
					} else {
						this.status = 'nomore';
						if (this.page > 2) {
							this.$refs.toast.show({
								type: 'error',
								title: '默认主题',
								message: "没有更多数据了"
							})
						}

					}
				}

			},
			getData(ee = false) {
				var that = this;
				getFlowName('list', {
					custom: {
						auth: true,
						// toast: false,
						catch: true,
					},
					params: {
						pageSize: this.pageSize,
						page: this.page,
						type: this.type,
					}
				}).then(e => {
					if (ee) {
						this.list = [...this.list, ...(e.data || [])]; // 安全连接数组
					} else {
						this.list = e.data || [];
					}
					this.status = 'loadmore';
					this.page = e.page;
					this.pageSize = e.pageSize;
					this.total = e.total;
				}).catch(e => {
					console.log(e);
				})
			},
			routed(url) {
				uni.$uv.route({
					url: url,
					// intercept(params, next) {
					// 	uni.showLoading({
					// 		title: '等待中...'
					// 	})
					// 	// 模拟处理等待
					// 	setTimeout(() => {
					// 		uni.hideLoading();
					// 		next(true);
					// 	}, 1000)
					// }
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.my-record {
		padding: 0px;
		width: 100%;

		// width: calc(100% - 20px);
		// padding: 10px;
		::v-deep .record_content {
			overflow: auto;
			padding: 0px;
			max-height: calc(100vh - 140px);

			.li_list {
				display: flex;
				// justify-content: space-between;
			}

			// background-color: #000;
			.record_list {
				// display: flex;
				// justify-content: space-between;
				// align-items: center;
				border: 2px solid #eeeeee;
				margin: 10px 5px;
				padding: 10px;
				background-color: rgba(255, 255, 255, 0.1);
				border-radius: 10px;
			}
		}

		::v-deep .uv-list-item {
			background-color: #000;
		}
	}
</style>